package com.wl156.activities.find;

import java.io.IOException;

import org.ksoap2.serialization.SoapObject;
import org.xmlpull.v1.XmlPullParserException;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.wl156.lite.R;
import com.wl156.activities.find.FindGoodMessageResult.FindTask;
import com.wl156.activities.find.FindResultBase.FindTaskBase;
import com.wl156.adapters.GoodAdapter;
import com.wl156.adapters.SpecialLineAdapter;
import com.wl156.adapters.TruckAdapter;

public class FindSpecialLineResult extends FindResultBase {

	String endLocationLv1;
	String endLocationLv2;
	String endLocationLv3;
	String daysDuringJourney;
	String daysTruckSchedule;
	String price1;
	String price2;

	boolean isAll = false;
	String userID;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mHeaderStub.setLayoutResource(R.layout.item_special_line_header);
		mHeaderStub.inflate();

		Bundle extras = getIntent().getExtras();
		startLocationLv1 = extras.getString("startLocationLv1");
		startLocationLv2 = extras.getString("startLocationLv2");
		startLocationLv3 = extras.getString("startLocationLv3");
		endLocationLv1 = extras.getString("endLocationLv1");
		endLocationLv2 = extras.getString("endLocationLv2");
		endLocationLv3 = extras.getString("endLocationLv3");
		daysDuringJourney = extras.getString("daysDuringJourney");
		daysTruckSchedule = extras.getString("daysTruckSchedule");
		price1 = extras.getString("price1");
		price2 = extras.getString("price2");

		if (extras.getBoolean("isAll")) {
			isAll = true;
			userID = extras.getString("userID");
			
			
		}

		new FindTask(type).execute();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
		Log.d(TAG, "onResume isAll: " + isAll);
		
		if(isAll) {
			hideBottomButtons();
			setTitle("所有专线");
		}
	}

	@Override
	protected String getFindName() {
		return "专线";
	}

	@Override
	protected Class getFindActivity() {
		return FindSpecialLine.class;
	}

	@Override
	protected Class getFindResultActivity() {
		return FindSpecialLineResult.class;
	}

	@Override
	protected int getPhoneIndex() {
		return 8;
	}

	@Override
	protected void onLastItemVisible() {
		super.onLastItemVisible();

		new FindTask(type).execute();
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);

		Intent i = new Intent(FindSpecialLineResult.this, SpecialLine.class);
		i.putExtra("id", id);
		startActivity(i);
	}

	public class FindTask extends FindTaskBase {
		public FindTask(int type) {
			super(type);
		}

		@Override
		protected void onLoadFailed() {
			super.onLoadFailed();

			new FindTask(type).execute();
		}

		@Override
		protected Object doInBackground(Void... params) {
			try {
				if (isAll) {
					return mWebServiceClient.company_findSpecialLines(
							mCertificate, userID, mCurrentPageIndex,
							countPerPage);
				} else {
					return mWebServiceClient.commonFindSpecialLines(
							mCertificate, startLocationLv1, startLocationLv2,
							startLocationLv3, endLocationLv1, endLocationLv2,
							endLocationLv3, daysDuringJourney,
							daysTruckSchedule, price1, price2,
							mCurrentPageIndex, countPerPage);
				}
			} catch (IOException e) {
				return e;
			} catch (XmlPullParserException e) {
				return e;
			}
		}
	}

	@Override
	protected Class getAdapter() {
		return SpecialLineAdapter.class;
	}
}
